package id.co.shuba.spamobile.util;

import id.co.shuba.spamobile.IDatePickerDialog;
import id.co.shuba.spamobile.R;
import id.co.shuba.spamobile.R.string;
import id.co.shuba.spamobile.model.ErrorInfo;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.InputType;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class ValidationUtil {
	public static void showValidationDialog(final Activity activity, final List<ErrorInfo> errors, final IDatePickerDialog dlg) {		
		CharSequence[] items = new CharSequence[errors.size()];
		int i = 0;
		for (ErrorInfo error : errors) 
			items[i++] = error.getMessage();					
		
		AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity,
				R.style.Theme_Sherlock_Light_Dialog));
		builder.setTitle(activity.getString(R.string.validation_dialog));
		builder.setItems(items, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {		    	
		    	View view = errors.get(item).getView();
		    	String notif = errors.get(item).getNotif();
		    	
		    	if (!Str.isNullOrEmpty(notif))
			    	Toast.makeText(activity, notif, Toast.LENGTH_LONG).show();
			    	
		    	if (view instanceof EditText) {
		    		EditText txt = (EditText)view;
		    		if (txt.getInputType() == (InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_DATE)) {
		    			//dlg.show(txt);
		    		} else {	    			
		    			view.requestFocus();
		    		}
		    	} else if (view instanceof Spinner) {
		    		if (!view.performClick())
		    			Log.e("Prospect Validation", "Perform click error???");
		    	}		    		    
		    }
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
